package com.ad.pet.common.structure.ui.control;

import android.content.Context;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ad.pet.R;


/**
 * Created by z on.
 * Des:
 * Date: 2017/3/9
 */

public class AppToolbar extends Toolbar {
private ImageView mIvRight;
    private TextView mTvTitle;
    private TextView mTvNext;
    private OnClickListener mOnClickListener;
    private OnClickListener mTitleIconClickListener;
    private OnClickListener mNextOnClickListener;
    private LinearLayout mLayoutBak;

    private View mView;
    private LayoutInflater mInflater;
    private ImageView mImageVBack;
    private LinearLayout mLayoutRigth;

    private TextView mIvTitleIcon;

    public AppToolbar(Context context) {
        this(context, null);
    }

    public AppToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews();
        initEvents();

    }

    private void initViews() {
        if (mView == null) {
            //初始化
            mInflater = LayoutInflater.from(getContext());
            mView = mInflater.inflate(R.layout.toolbar_layout, this, false);
        }
        mTvTitle =  mView.findViewById(R.id.tv_bar_title);
        mTvNext =  mView.findViewById(R.id.but_next);
        mLayoutBak =  mView.findViewById(R.id.layout_back);
        mImageVBack =  mView.findViewById(R.id.iv_back);
        mLayoutRigth =  mView.findViewById(R.id.layout_right);
        mIvTitleIcon = mView.findViewById(R.id.iv_title_icon);

        mIvRight = mView.findViewById(R.id.ivNext);
        addView(mView);
    }

    private void initEvents() {
        mTvNext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNextOnClickListener != null) mNextOnClickListener.onClick(mTvNext);
            }
        });

        mLayoutBak.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) mOnClickListener.onClick(mLayoutBak);
            }
        });
        mIvTitleIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTitleIconClickListener!=null)
                    mTitleIconClickListener.onClick(mIvTitleIcon);
            }
        });
    }


    @Override
    public void setTitle(CharSequence title) {
        if (getResources().getString(R.string.app_name).equals(title)) return;
        mTvTitle.setText(title);

//        mIvLine.setVisibility(VISIBLE);
    }

    public CharSequence getTitle() {
        return mTvTitle.getText();
    }

    @Override
    public void setTitleTextColor(@ColorInt int color) {
        if (color == -1) return;
        mTvTitle.setTextColor(color);
    }

    public void setBackOnClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            this.mOnClickListener = onClickListener;
        }
    }

    public void setNextOnClickListener(OnClickListener onClickListener) {
        if (mTvNext.getVisibility() == GONE) mTvNext.setVisibility(VISIBLE);
        if (onClickListener != null) {
            this.mNextOnClickListener = onClickListener;
        }
    }

    public void  setIconTitle(int num){
        mIvTitleIcon.setText(String.valueOf(num));
    }
    public void setNextTextColor(int color) {
        if (mTvNext.getVisibility() == GONE) mTvNext.setVisibility(VISIBLE);
        mTvNext.setTextColor(color);
    }

    public void setNextOnClickListener(OnClickListener onClickListener, String text) {
        if (mTvNext.getVisibility() == GONE) mTvNext.setVisibility(VISIBLE);
        if (onClickListener != null) {
            this.mNextOnClickListener = onClickListener;
        }
        mTvNext.setText(text);
    }

    public void setTitleIconClickListener(OnClickListener titleIconClickListener) {
        if (mIvTitleIcon.getVisibility() == GONE) mIvTitleIcon.setVisibility(VISIBLE);
        if (titleIconClickListener != null) {
            mTitleIconClickListener = titleIconClickListener;
        }

    }
    public View getTitleIcon(){
        return mIvTitleIcon;
    }

    public void setNextText(String text) {
        if (mTvNext.getVisibility() == GONE) mTvNext.setVisibility(VISIBLE);
        mTvNext.setText(text);
    }

    public  View getLeftIcon(){
        return mImageVBack;
    }

    public void isDispayBackIcon(boolean is) {
        mLayoutBak.setVisibility(is ? VISIBLE : GONE);
    }

    public void setLeftEnabled() {
        mLayoutBak.setOnClickListener(null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mLayoutBak.setForeground(null);
        }
    }

    public void hideRigth() {
        mTvNext.setVisibility(GONE);
    }

    public View getRigthView() {
        return mTvNext;
    }


    public void addRightView(View view) {
        mLayoutRigth.addView(view);
    }

    public void setLeftIcon(int image){
        (( ImageView)getLeftIcon()).setImageResource(image);
    }

    public ImageView getRightImage(){
        return mIvRight;
    }

    public TextView getTitleView(){
        return mTvTitle;
    }
}
